package com.sanjiayi.controller;

import com.sanjiayi.dto.NearbyUserDTO;
import com.sanjiayi.pojo.User;
import com.sanjiayi.result.Result;
import com.sanjiayi.service.NearbyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/nearby")
@Api(tags = "同城相关接口")
public class NearbyController {

    @Autowired
    private NearbyService nearbyService;

    @PostMapping("/location")
    @ApiOperation("更新用户位置")
    public Result updateLocation(@RequestBody NearbyUserDTO nearbyUserDTO) {
        try {
            nearbyService.updateLocation(nearbyUserDTO);
            return Result.ok("位置更新成功");
        } catch (Exception e) {
            log.error("更新位置失败", e);
            return Result.error("位置更新失败");
        }
    }

    @GetMapping("/users")
    @ApiOperation("查询附近的用户")
    public Result findNearbyUsers(NearbyUserDTO nearbyUserDTO) {
        try {
            List<User> users = nearbyService.findNearbyUsers(nearbyUserDTO);
            return Result.ok(users);
        } catch (Exception e) {
            log.error("查询附近用户失败", e);
            return Result.error("查询附近用户失败");
        }
    }
} 